//
//  ComponentVC.swift
//  Example
//
//  Created by remy on 2020/1/17.
//  Copyright © 2020 com.Risac. All rights reserved.
//

class ComponentVC: BaseVC {
    
    private lazy var textCountView = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: 50.0, height: 25.0), textColor: FlatColor.midnightBlue, fontSize: 12.0, alignment: .right)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text(title)
        let scrollView = RisacScrollView(frame: self.bodyViewFrame, bgColor: FlatColor.turquoise)
        scrollView.alwaysBounceVertical = true
        self.view.addSubview(scrollView)
        let textView = RisacTextView(frame: CGRect(x: 16.0, y: 16.0, width: Size.screenWidth - 32.0, height: 120.0)) {
            $0.backgroundColor = FlatColor.midnightBlue
            $0.contentInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 25.0, right: 10.0)
            $0.maxTextLength = 40
            $0.delegate = self
            $0.placeholderLabel.text = "请输入...(\($0.maxTextLength))以内"
            $0.placeholderLabel.textColor = FlatColor.midnightBlue
            $1.textColor = FlatColor.midnightBlue
            $1.font = UIFont.systemFont(ofSize: 14.0)
            $1.textContainerInset = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
            $1.backgroundColor = FlatColor.sunflower
        }
        textCountView.left = textView.width - textCountView.width - 10.0
        textCountView.top = textView.height - textCountView.height
        textView.addSubview(textCountView)
        scrollView.addSubview(textView)
        let textFieldView = RisacTextFieldView(frame: CGRect(x: 16.0, y: textView.bottom + 16.0, width: Size.screenWidth - 32.0, height: 50.0)) {
            $0.backgroundColor = FlatColor.midnightBlue
            $0.contentInset = UIEdgeInsets(top: 4.0, left: 4.0, bottom: 4.0, right: 4.0)
            $0.delegate = self
            $1.attributedPlaceholder = ("请输入".sac.attrWrap.color(FlatColor.midnightBlue).baseline(-2.0) + "请输入".sac.attrWrap.color(FlatColor.carrot).baseline(2.0)).rawValue
            $1.keyboardType = .default
            $1.returnKeyType = .send
            $1.textColor = FlatColor.midnightBlue
            $1.font = UIFont.systemFont(ofSize: 14.0)
            $1.backgroundColor = FlatColor.sunflower
        }
        scrollView.addSubview(textFieldView)
        scrollView.contentSize = CGSize(width: 0.0, height: scrollView.subviews.last!.bottom + Size.bottomExtraHeight + 20.0)
    }
}

extension ComponentVC: RisacTextViewDelegate {
    
    func textViewEditBegin(view: RisacTextView, textView: UITextView) {
        Risac.print("textViewEditBegin")
    }
    
    func textViewEditEnd(view: RisacTextView, textView: UITextView) {
        Risac.print("textViewEditEnd")
    }
    
    func textViewEditChanged(view: RisacTextView, textView: UITextView) {
        Risac.print("textViewEditChanged")
    }
    
    func textViewHeightChanged(view: RisacTextView, textView: UITextView, height: CGFloat) {
        Risac.print("textViewHeightChanged: \(height)")
    }
    
    func textViewCountChanged(view: RisacTextView, textView: UITextView, count: (Int, Int)) {
        Risac.print("textViewCountChanged: \(count)")
        textCountView.attributedText = ("\(count.1)".sac.attrWrap.color(count.1 > 0 ? FlatColor.turquoise : FlatColor.carrot) + " / \(view.maxTextLength)".sac.attrWrap.color(FlatColor.turquoise)).rawValue
    }
}

extension ComponentVC: RisacTextFieldViewDelegate {
    
    func textFieldEditBegin(view: RisacTextFieldView, textField: UITextField) {
        Risac.print("textFieldEditBegin")
    }
    
    func textFieldEditEnd(view: RisacTextFieldView, textField: UITextField) {
        Risac.print("textFieldEditEnd")
    }
    
    func textFieldEditChanged(view: RisacTextFieldView, textField: UITextField) {
        Risac.print("textFieldEditChanged")
    }
    
    func textFieldReturn(view: RisacTextFieldView, textField: UITextField) {
        Risac.print("textFieldReturn")
    }
}
